#include <cstdint>
#include <deque>
#include <iostream>
#include <istream>
#include <vector>
using ll = int64_t;

const ll maxn = 5e5+5;
ll n,ans,dp[maxn],f[maxn],k[maxn];
char c[maxn];
std::vector<std::vector<ll>> s;
std::vector<ll> stk;

static inline void dfs(ll u){
    ll pos =0;
    bool ispushed = false;
    if(c[u]=='('){
        stk.push_back(u);
        ispushed=true;
    }else if(stk.size()){
        pos = stk.back();
        stk.pop_back();
        dp[u]=dp[f[pos]]+1;
    }
    k[u]=k[f[u]]+dp[u];
    ans^=u*k[u];
    for(ll v:s[u]){
        dfs(v);
    }
    if(ispushed){
        stk.pop_back();
    }
    if(pos){
        stk.push_back(pos);
    }
}

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin>>n;
    s.resize(n+1);
    for(ll i=1;i<=n;i++){
        std::cin>>c[i];
    }
    for(ll i=2;i<=n;i++){
        ll tmp;
        std::cin>>tmp;
        s[tmp].push_back(i);
        f[i]=tmp;
    }
    dfs(1);
    std::cout<<ans<<'\n';
}